import pandas as pd
import re
from typing import List

# 该文件，是初次解析文件，将数据拆分为2个待处理的表

# 拆表，保存数据 word.xlsx和phrase.xlsx

def write_data_to_xlsx(data: List, columns, file_name):
    pd.DataFrame(data, columns=columns).to_excel(r'./split_table_data/{0}.xlsx'.format(file_name),
                                                 sheet_name='Sheet1',
                                                 index=False,
                                                 header=True
                                                 )


def parse_xls():
    df = pd.read_excel(r'./files/toDeal.xlsx', sheet_name='fb_word_detail', header=None)  # sheet_name 读取那个工作表
    data = df.values  # 全部数据
    header = data[0]  # 读取第一行的列名
    other_lines = data[1:]  # 除第一行外剩余数据
    result = [dict(zip(header, line)) for line in other_lines]

    phrase_list = []
    word_list = []
    for i in result:
        word = i.get('word').strip()
        if '-' in word or re.search(r'\s', word):  # 读取到phrase.xlsx
            phrase_list.append(list(i.values()))
        else:  # 读取到word.xlsx
            word_list.append(list(i.values()))
    write_data_to_xlsx(phrase_list, header, 'phrase')
    write_data_to_xlsx(word_list, header, 'word')


if __name__ == '__main__':
    parse_xls()
